require("dotenv").config();
const express = require("express");
const mongoose = require("mongoose");
const cors = require("cors");

// Import Routes
const authRoutes = require("./routes/authRoutes");
const userRoutes = require("./routes/userRoutes");
const UsersRoutes = require("./routes/UsersRoutes");
const roleRoutes = require("./routes/roleRoutes");
const storeRoutes = require("./routes/storeRoutes");
const messageRoutes = require("./routes/messageRoutes");
const customerRoutes = require("./routes/customerRoutes");
const deliveryAgentRoutes = require("./routes/deliveryAgentRoutes");
const categoryRoutes = require("./routes/categoryRoutes");
const itemRoutes = require("./routes/itemRoutes");
const supplierRoutes = require("./routes/supplierRoutes");
const purchaseRoutes = require("./routes/purchaseRoutes");
const serviceRoutes = require("./routes/serviceRoutes");
const brandRoutes = require("./routes/brandRoutes");
const variantRoutes = require("./routes/variantRoutes");
const expenseRoutes = require("./routes/expenseRoutes");
const warehouseRoutes = require("./routes/warehouseRoutes");
const stockAdjustmentRoutes = require("./routes/stockAdjustmentRoutes");
const stockTransferRoutes = require("./routes/stockTransferRoutes");
const taxRoutes = require("./routes/taxRoutes");
const unitRoutes = require("./routes/unitRoutes");
const paymentTypeRoutes = require("./routes/paymentTypeRoutes");
const customerDataRoutes = require("./routes/customerDataRoutes");
const advancePaymentRoutes = require("./routes/advancePaymentRoutes");
const accountRoutes = require("./routes/accountRoutes");
const moneyTransferRoutes = require("./routes/moneyTransferRoutes");
const depositRoutes = require("./routes/depositRoutes");
const cashTransactionRoutes = require("./routes/cashTransactionRoutes.js");
const posRoutes = require("./routes/posRoutes");
const salesRoutes = require("./routes/salesRoutes");
const salesReturnRoutes = require("./routes/salesReturnRoutes");
const discountCouponRoutes = require("./routes/discountCouponRoutes");
const customerCouponRoutes = require("./routes/customerCouponRoutes");
const quotationRoutes = require("./routes/quotationRoutes");
const expenseCategoryRoutes = require("./routes/expenseCategoryRoutes");
const dashboardRoutes = require("./routes/dashboardRoutes");
const posSettingsRoutes = require("./routes/posSettingsRoutes");
const countryStateRoutes = require("./routes/countryStateRoutes");
const subscriptionRoutes = require("./routes/subscriptionRoutes");
const app = express();


app.use(cors({
  origin: function (origin, callback) {
    // Allow requests with no origin (like mobile apps or curl requests)
    if (!origin) return callback(null, true);
    // Reflect the origin back in the response
    callback(null, origin);
  },
  credentials: true,
}));




app.use(express.json());
app.use(express.urlencoded({ extended: true }));

// Connect to MongoDB
mongoose.connect(process.env.MONGO_URI, {
  useNewUrlParser: true,
  useUnifiedTopology: true,
})
  .then(() => console.log("✅ MongoDB Connected"))
  .catch(err => console.error("❌ MongoDB Connection Error:", err));


console.log("🔑 JWT_SECRET:", process.env.JWT_SECRET);


app.use("/auth", authRoutes);
app.use("/admin", userRoutes);
app.use("/admiaddinguser", UsersRoutes);
app.use("/admincreatingrole", roleRoutes);
app.use("/admin/store", storeRoutes);
app.use("/api/message", messageRoutes);
app.use("/customer", customerRoutes);
app.use("/delivery-agent", deliveryAgentRoutes);
app.use("/api/categories", categoryRoutes);
app.use("/api", itemRoutes);
app.use("/api/suppliers", supplierRoutes);
app.use("/api/purchases", purchaseRoutes);
app.use("/api/services", serviceRoutes);
app.use("/api/brands", brandRoutes);
app.use("/api/variants", variantRoutes);
app.use("/api/expenses", expenseRoutes);
app.use("/api/expense-categories", expenseCategoryRoutes);
app.use("/api", warehouseRoutes);
app.use("/api/stock-adjustments", stockAdjustmentRoutes);
app.use("/api/stock-transfers", stockTransferRoutes);
app.use("/api", taxRoutes);
app.use("/api", unitRoutes);
app.use("/api", paymentTypeRoutes);
app.use("/api/customer-data", customerDataRoutes);
app.use("/api/advance-payments", advancePaymentRoutes);
app.use("/api/accounts", accountRoutes);
app.use("/api/money-transfers", moneyTransferRoutes);
app.use("/api/deposits", depositRoutes);
app.use("/api/cash-transactions", cashTransactionRoutes);
app.use("/api/pos", posRoutes);
app.use("/api/sales", salesRoutes);
app.use("/api/sales-return", salesReturnRoutes);
app.use("/api/discount-coupons", discountCouponRoutes);
app.use("/api/customer-coupons", customerCouponRoutes);
app.use("/api/quotation", quotationRoutes);
app.use("/api", dashboardRoutes);
app.use("/api/pos-settings", posSettingsRoutes);
app.use("/api", countryStateRoutes);
app.use("/api", subscriptionRoutes);


const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
  console.log(`🚀 Server running on port ${PORT}`);
});
